package com.offcn.question.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.offcn.question.entity.QuestionEntity;
import com.offcn.question.service.QuestionService;
import com.offcn.common.utils.PageUtils;
import com.offcn.common.utils.R;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;


/**
 * 
 *
 * @author Since wu
 * @email 2676697841@qq.com
 * @date 2021-07-21 16:42:50
 */
@RestController
@RequestMapping("question/question")
public class QuestionController {
    @Autowired
    private QuestionService questionService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = questionService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		QuestionEntity question = questionService.getById(id);

        return R.ok().put("question", question);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody QuestionEntity question){
		questionService.save(question);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody QuestionEntity question){
		questionService.updateById(question);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		questionService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 题目上传导入
     * @return
     */
    @PostMapping("/upload")
    public R upload(MultipartFile file){
        Map result = questionService.importExcel(file);
        return R.ok().put("result",result.get("result")).put("msg",result.get("msg")).put("num",result.get("num"));
    }

    /**
     * 题目下载导出
     * @param response
     */
    @GetMapping("/exportExcel")
    public void export(HttpServletResponse response){
        questionService.exportExcel(response);
    }

    /**
     * 题目类型统计
     */
    @GetMapping("/countTypeQuestion")
    public R countTypeQuestion(){
        List<Map<String, Object>> mapList = questionService.countTypeQuestion();
        return R.ok().put("mapList",mapList);
    }
}
